/**
 * Created by zhangyanhui on 2017/6/20.
 * 设置缓存
 */
var Cache = {
    /**
     *
     * @param key
     * @param obj
     * @param expire 过期单位秒,0永久
     */
    set: function (key, obj, expire) {
        var storage = Cache.getLocalStorage();
        if (storage != null){
            var cacheObj = {
                value: obj,
                expire: expire * 1000,
                timestamp:new Date().getTime()
            };
            storage.setItem(key, JSON.stringify(cacheObj));
            return true;
        }
        return false;
    },
    get: function (key) {
        var result = null;
        var storage = Cache.getLocalStorage();
        if (storage != null) {
            var jsonStr = storage.getItem(key);
            if(jsonStr != null && jsonStr != "" && jsonStr != undefined){
                var data = JSON.parse(jsonStr);
                if (data != null && data != undefined){
                    if (data.expire <= 0 || new Date().getTime() < data.timestamp + data.expire){
                        result = data.value;
                    }
                }
            }
        }
        return result;
    },
    del: function (key) {
        var storage = Cache.getLocalStorage();
        if (storage != null){
            storage.removeItem(key);
            return true;
        }
        return false;

    },
    /**
     * 浏览器不支持LocalStorage storage=null
     */
    getLocalStorage: function () {
        var storage = window.localStorage;
        if(!storage){
            storage = null;
        }
        return storage;
    }

};

var util = {
	//基础参数
	baseURL: "http://106.75.101.210:9092",//测试
	
	stopDownRefresh: function(id){
		mui('#'+id).pullRefresh().endPulldownToRefresh();
	},
	stopUpRefresh: function(id){
		mui('#'+id).pullRefresh().endPullupToRefresh();
	},
	
	
	/**
	 * 
	 * 上拉更多 和 下拉刷新功能
	 *
	 * 示例：
	 * 
	 var refreshId = "appendHtmlId";
		mui.init({
			pullRefresh: util.pullRefresh(refreshId, function(){
				//下拉刷新
				util.stopDownRefresh(refreshId)
				
			}, function(){
				// 上拉更多
				util.stopUpRefresh(refreshId)
			})
		})
	 */
	
	pullRefresh: function(id, downCallback, upCallback){
		return {
			container: '#'+id,
			down: {
				style: 'circle',
				offset: '0px',
				auto: true,
				callback: downCallback
				
			},
			up: {
				contentrefresh: '正在加载...',
				callback: upCallback
			}
		};
	},
	
	//公共方法
	openWin: function(url){
		mui.openWindow({
			id: url,
			url: url
		});
	},
	
	/**
	 * url : 请求地址
	 * param : 参数
	 * {
			username:'username',
			password:'password'
		}
		okFun: 成功方法, 参数为result对象
	 */
	post: function(url, param, okFun){
		mui.ajax(util.baseURL + url,{
			data:param,
			dataType:'json',//服务器返回json格式数据
			type:'post',//HTTP请求类型
			timeout:10000,//超时时间设置为10秒；
			headers:{'Content-Type':'application/json'},	              
			success:function(data){
				//服务器返回响应，根据响应结果，分析是否登录成功；
				util.handleResponse(data, okFun);
			},
			error:function(xhr,type,errorThrown){
				//异常处理；
				util.error("网络异常");
			}
		});
	},
	/**
	 * 弹框
	 */
	alert: function(title){
		mui.alert(title, function() {});
	},
	/**
	* 系统错误
	*/
	error: function(title){
		mui.alert(title, function() {});
	},
	/**
	 * 土司消息
	 */
	toast: function(content){
		mui.toast(content);
	},
	handleResponse: function(result, okFun) {
		if(result.status == '1'){
			if(typeof(okFun) == 'function'){
				okFun(result.result)
			}
		}else{
			util.alert(result.message)	
		}
	}
	
	
}
